<?php
/**
 *  
 * @package package_name
 * @author Mervin.Wong<mervin0502@163.com>
 * @copyright
 * @license  http://www.apache.org/licenses/LICENSE-2.0
 * @version 0.1.0
 * @date 2013-3-2下午9:31:32
 *
 */
 
 /**
  * CanGoo Lastest_post widget
  * 最新文章
  * @package blog
  * @subpackage libraries
  * @category widget
  * @author mervin.wong
  * */
 class Lastest_post{
 		/**
	 * 超级对象
	 */
	private $_CI;
	
	
	/**
	 * 排序
	 */
	public $orderby = array();
	/**
	 * 输出的数量
	 */
	public $count = 10;
	
	/**
	 * 构造函数
	 */
	public function __construct(){
		$this->_CI = & get_instance();
		$this->_CI->load->database();
		$this->_CI->load->helper('url');
		$this->_CI->load->helper('common');
	}
	/**
	 * 魔法函数，设置和获取变量
	 */
	public function __get($name){
		return $this->$name;
	}
	public function __set($name, $value){
		$this->$name = $value;
	}
	public function run(){
		$return = '';
		if(($posts = $this->__get_posts()) != false){
			foreach($posts as $v){
				$return .= '<li><a href="'.site_url('article/index/'.$v['bpid']).'" title="'.$v['excerpt'].'">'.cut_str($v['title'], 0, 23, 'UTF-8').'</a><span>浏览'.$v['viewNum'].'次</span>';
			}
			return $return;
		}else{
			return false;			
		}
	}
	
	/**
	 * select the tag by the coditions
	 * @access private
	 * @param void paramname
	 * @return array|bool description
	 */
	private function __get_posts(){
		$db = $this->_CI->db;
		$db->select(array('bpid','title', 'slug', 'excerpt', 'viewNum', 'commentNum'));
		$db->from('blog_post');
		if(count($this->orderby) > 0){
			foreach($this->orderby as $k=>$v){
				$db->order_by($k,$v);
			}
		}else{
			$db->order_by('addTime', 'DESC');
		}
		$db->limit($this->count);
		$query = $db->get();
		if($query->num_rows > 0){
			return $query->result_array();
		}else{
			return false;
		}
	}
 }